<template>
  <a-modal
    destroyOnClose
    :title='title'
    :width='500'
    :visible='visible'
    :confirmLoading='confirmLoading'
    :ok-button-props="{ props: { disabled: disableSubmit } }"
    @ok='handleOk'
    @cancel='handleCancel'>
    <a-spin :spinning='confirmLoading'>
      <div style="margin: 0 auto" :class="{ isCheck: isCheck }">
        <a-form-model ref='form' :label-col='labelCol' :wrapper-col='wrapperCol' :model='model' :rules='validatorRules'>
          <!-- 主表单区域 -->
          <a-form-model-item label='资料名称' prop='stageName'>
            <a-input v-model='model.stageName' :disabled='disableSubmit'/>
          </a-form-model-item>
          <a-form-model-item label='是否必做' prop='ismust'>
            <a-radio-group v-model='model.ismust' :disabled='disableSubmit'>
              <a-radio value='1'>
                是
              </a-radio>
              <a-radio value='2'>
                否
              </a-radio>
            </a-radio-group>
          </a-form-model-item>
          <a-form-model-item label='资料类型' prop='tempName'>
            <a-select
              v-model='model.tempId'
              placeholder='请选择'
              :disabled='disableSubmit'
            >
              <a-select-option key='0' value='0'>
                手动上传
              </a-select-option>
              <a-select-option v-for='item in mubanList' :key='item.id' :value='item.id'>
                {{ item.name }}
              </a-select-option>
            </a-select>
          </a-form-model-item>
          <a-form-model-item v-if='uploadModelFlag||model.tempId == null' label='上传模板'>
            <MeansUpload :isShow="disableSubmit" ref='fileUpload1' :fileData='tempFileData' :file-url='fileUrl'
                         @removeFile="()=>{removeFile('1')}"
                         @returnFileId="(id)=>{ this.model.fileId = id }"></MeansUpload>
          </a-form-model-item>
          <a-form-model-item label='资料上传'>
            <MeansUpload :isShow="disableSubmit" ref='fileUpload2' :fileData='jdxwsFileData' :file-url='fileUrl'
                         @removeFile="()=>{removeFile('2')}"
                         @returnFileId="(id)=>{ this.model.jdxwsId = id }"></MeansUpload>
          </a-form-model-item>
        </a-form-model>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
  import {httpAction, getAction, uploadAction} from '@/api/manage'
  import MeansUpload from '../../../components/MeansUpload'

  export default {
    name: 'JeecgOrderMainModal',
    props: ['id', 'stage'],
    components: {
      MeansUpload
    },
    data() {
      return {
        title: '操作',
        visible: false,
        model: {},
        uploadModelFlag: false,
        disableSubmit: false,
        isCheck: false,
        labelCol: {
          xs: {span: 24},
          sm: {span: 5}
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16}
        },
        confirmLoading: false,
        validatorRules: {
          stageName: [
            {required: true, message: '请输入', trigger: 'blur'}
          ],
          ismust: [
            {required: true, message: '请输入', trigger: 'blur'}
          ]
        },
        url: {
          add: '/sjxmzygl/sjWorkBcxsjws/add',
          edit: '/sjxmzygl/sjWorkBcxsjws/edit',
          muban: '/xtsz/sjWordTemplate/queryWordTempList'
        },
        fileUrl: {
          saveFile: '/sys/common/sjUpload',
          // removeFile: '/sjzyzhgl/sjFileInfo/removeFile'
        },
        mubanList: [],
        tempFileData: {},
        jdxwsFileData: {}
      }
    },
    created() {
      this.getMubanList()
    },
    watch: {
      'model.tempId': {
        handler: function (val) {
          if (val == '0') {
            this.model.tempId = '0'
            this.uploadModelFlag = true
          } else {
            this.model.fileId = ''
            this.uploadModelFlag = false
          }
        },
        immediate: true,
        deep: true
      }
    },
    methods: {
      add() {
        this.visible = true
        this.model = {
          tempId: '0'
        }
        this.tempFileData = {}
        this.jdxwsFileData = {}
      },
      check(record) {
        this.visible = true
        this.model = Object.assign({}, record)
        this.model.tempId = record.tempId || '0'
        this.model.ismust = record.ismust.toString()
        this.tempFileData.affixId = record.fileId
        this.tempFileData.fileName = record.tempName
        this.tempFileData.fileUrl = record.tempUrl
        this.jdxwsFileData.affixId = record.jdxwsId
        this.jdxwsFileData.fileName = record.jdxwsName
        this.jdxwsFileData.fileUrl = record.jdxwsUrl
      },
      edit(record) {
        this.visible = true
        this.model = Object.assign({}, record)
        this.model.tempId = record.tempId || '0'
        this.model.ismust = record.ismust.toString()
        this.tempFileData.affixId = record.fileId
        this.tempFileData.fileName = record.tempName
        this.tempFileData.fileUrl = record.tempUrl
        this.jdxwsFileData.affixId = record.jdxwsId
        this.jdxwsFileData.fileName = record.jdxwsName
        this.jdxwsFileData.fileUrl = record.jdxwsUrl
      },
      close() {
        this.$emit('close')
        this.visible = false
      },
      removeFile(type) {
        if (type === '1') {
          this.model.fileId = ''
          this.model.tempId = '0'
        } else {
          this.model.jdxwsId = ''
        }
      },
      saveData() {
        let httpurl, method, param

        if (!this.model.id) {
          httpurl = this.url.add
          method = 'post'
          param = {
            proId: this.id,
            stage: this.stage,
            ...this.model
          }
        } else {
          httpurl = this.url.edit
          method = 'put'
          param = {
            id: this.model.id,
            proId: this.id,
            stage: this.stage,
            ...this.model
          }
        }
        httpAction(httpurl, param, method).then((res) => {
          this.confirmLoading = false
          if (res.success) {
            this.$message.success(res.message)
            this.$emit('ok')
            this.visible = false
          } else {
            this.$message.error(res.message)
          }
        })
      },
      handleOk() {
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            this.confirmLoading = true
            this.saveData()
          }
        })
      },
      handleCancel() {
        this.close()
      },
      getMubanList() {
        getAction(this.url.muban).then((res) => { //获取审核人列表
          if (res.success && res.result) {
            this.mubanList = res.result
          } else {
            console.log(res)
          }
        }).finally(() => {
        })
      },
      GetChinese(strValue) {
        if (strValue != null && strValue != '') {
          var reg = /[\u4e00-\u9fa5]/g
          return strValue.match(reg).join('')
        } else
          return ''
      }
    }
  }
</script>
<style lang="less" scoped>
  @import '../../../css/checkForm.less';
</style>
<style scoped>

  .ant-btn {
    padding: 0 10px;
    margin-left: 3px;
  }

  .ant-form-item-control {
    line-height: 0px;
  }

  /** 主表单行间距 */
  .ant-form .ant-form-item {
    margin-bottom: 10px;
  }

  /** Tab页面行间距 */
  .ant-tabs-content .ant-form-item {
    margin-bottom: 0px;
  }
</style>
